package com.example.jetpacktest.livedata

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.example.jetpacktest.R
import kotlinx.android.synthetic.main.activity_live_data_example.*
import org.jetbrains.anko.startActivity

class LiveDataExampleActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_live_data_example)
        liveDataInFragment?.setOnClickListener {
            startActivity<LiveDataWithFragmentInActivity>()
        }
        var viewModel = ViewModelProvider(this).get(TimerWithLiveDataViewModel::class.java)
        viewModel?.count?.observe(this, object : Observer<Int> {
            override fun onChanged(t: Int?) {
                runOnUiThread {
                    showCount?.text = t?.toString()
                }
            }
        })
        viewModel?.startCount()
    }
}
